package com.elitel.frame.base.service.strategy.service;

import com.elitel.frame.main.dao.CfgSimInfoMapper;
import com.elitel.frame.main.dao.CfgSimQueryFieldMapper;
import com.elitel.frame.main.entity.CfgSimInfo;
import com.elitel.frame.main.entity.CfgSimQueryField;
import com.elitel.frame.main.entity.enums.BuildServiceEnum;
import com.elitel.frame.main.entity.ext.PrjServiceExt;
import com.elitel.frame.main.entity.vo.ServiceConfig;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * 单表服务策略
 *
 * @author xqh
 * @date 2025/02/07
 */
@Component
@Slf4j
public class SimServerStrategy extends BuildServiceStrategy {


    private static final String FIELD_DATA_TYPE_STRING = "String";

    @Resource
    CfgSimInfoMapper cfgSimInfoMapper;
    @Resource
    CfgSimQueryFieldMapper cfgSimQueryFieldMapper;

    /**
     * 获取策略
     *
     * @return 策略枚举
     */
    protected BuildServiceEnum getTypeEnum() {
        return BuildServiceEnum.simServer;
    }

    @Override
    public List<ServiceConfig> buildServiceModel(List<PrjServiceExt> prjServiceExtList) {

        // 收集所有需要处理的serviceId
        List<String> serviceGuids = prjServiceExtList.stream()
                .map(PrjServiceExt::getGuid)
                .collect(Collectors.toList());

        // 批量查询CfgSimInfo
        Map<String, CfgSimInfo> simInfoMap = cfgSimInfoMapper.selectByGuids(serviceGuids).stream()
                .collect(Collectors.toMap(CfgSimInfo::getServiceId, Function.identity()));

        // 批量查询CfgSimQueryField
        Map<String, List<CfgSimQueryField>> queryFieldMap = cfgSimQueryFieldMapper.selectByGuids(serviceGuids).stream()
                .collect(Collectors.groupingBy(CfgSimQueryField::getServiceId));

        List<ServiceConfig> serviceList = new ArrayList<>();
        for (PrjServiceExt service : prjServiceExtList) {

            ServiceConfig serviceConfig = getBaseServiceMap(service);
            serviceConfig.setPageFlag(service.getPaginationFlag());

            CfgSimInfo extInfo = simInfoMap.get(service.getGuid());

            ServiceConfig.ExtInfo extInfoMap = new ServiceConfig.ExtInfo();
            extInfoMap.setRequestMethod(extInfo.getRequestMethod());
            extInfoMap.setRequestUrl(extInfo.getRequestUrl());
            extInfoMap.setRequestHeader(extInfo.getRequestHeader());
            extInfoMap.setContentType(extInfo.getContentType());
            extInfoMap.setOptType(extInfo.getOptType());

            serviceConfig.setExtInfo(extInfoMap);

            // 查询服务查询字段
            List<CfgSimQueryField> cfgSimQueryFields = queryFieldMap.get(service.getGuid());
            List<ServiceConfig.QueryParam> queryFieldList = buildQueryFieldList(cfgSimQueryFields);
            if (!ObjectUtils.isEmpty(queryFieldList)) {
                serviceConfig.setQueryParam(queryFieldList);
            }

            serviceList.add(serviceConfig);
        }
        return serviceList;
    }

    // 构建查询字段列表
    private List<ServiceConfig.QueryParam> buildQueryFieldList(List<CfgSimQueryField> cfgSimQueryFieldList) {
        if (ObjectUtils.isEmpty(cfgSimQueryFieldList)) {
            return Collections.emptyList();
        }

        List<ServiceConfig.QueryParam> queryParamList = new ArrayList<>();
        cfgSimQueryFieldList.forEach(queryField -> {
            ServiceConfig.QueryParam queryParam = new ServiceConfig.QueryParam();

            queryParam.setFieldCode(queryField.getNewFieldCode());
            queryParam.setFieldName(queryField.getNewFieldName());
            queryParam.setSourceFieldCode(queryField.getSourceFieldCode());
            queryParam.setSourceFieldName(queryField.getSourceFieldCode());
            queryParam.setRequiredFlag(0);
            queryParam.setDataType(FIELD_DATA_TYPE_STRING);
            queryParam.setDateFormat(null);
            queryParam.setDefaultValue(null);
            queryParam.setParamType(queryField.getParamType());
            queryParam.setDefaultValue(queryField.getDefaultValue());

            queryParamList.add(queryParam);
        });

        return queryParamList;
    }


}
